package com.ruoyi.system.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.*;


public class MdzwUtils {

    public static Map<String, Object> parseJsonToMap(JSONObject json) {
        if (json == null) {
            return new HashMap<>();
        }

        Map<String, Object> map = new HashMap<>();
        for (Map.Entry<String, Object> entry : json.entrySet()) {
            Object value = entry.getValue();
            String key = entry.getKey();

            if (value instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<>();
                JSONArray array = (JSONArray) value;
                for (Object item : array) {
                    if (item instanceof JSONObject) {
                        list.add(parseJsonToMap((JSONObject) item));
                    } else {
                        list.add(Collections.singletonMap(key, item));
                    }
                }
                map.put(key, list);
            } else if (value instanceof JSONObject) {
                map.put(key, parseJsonToMap((JSONObject) value));
            } else {
                map.put(key, value);
            }
        }
        return map;
    }


}

